package com.guo.xiao.bing.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guo.xiao.bing.common.Config;
import com.guo.xiao.bing.dao.NewsMapper;
import com.guo.xiao.bing.entity.News;
import com.guo.xiao.bing.entity.NewsExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NewsService {

    @Autowired
    NewsMapper newsMapper;

    public PageInfo<News> find(Integer currentPage) {

        NewsExample ne = new NewsExample();
        ne.setOrderByClause("id desc");

        PageHelper.startPage(currentPage, Config.NEWS_PAGE_SIZE);
        List<News> users = newsMapper.selectByExampleWithBLOBs(ne);
        PageInfo<News> pageInfo = new PageInfo<>(users);

        return pageInfo;
    }

    public News findById(Integer id) {
        return newsMapper.selectByPrimaryKey(id);
    }

    public Integer save(News news) {
        return newsMapper.insert(news);
    }

    public Integer update(News news) {
        return newsMapper.updateByPrimaryKeySelective(news);
    }

    public Integer delete(Integer id) {
        return newsMapper.deleteByPrimaryKey(id);
    }
}
